package com.hanlin.day06;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author:hl.yuan
 * @Date:2024/7/16 21:51
 * @Description: 单向队列实现栈
 * 解题思路：使用两个单向队列，实现栈，数据相互倒
 **/
public class QueueToStack {


    private static Queue<Integer> queue =  new LinkedList<>();
    private static Queue<Integer> helper = new LinkedList<>();

    /**
     * 添加数据
     * @param value
     */
    public static void push(int value) {
        queue.offer(value);
    }

    /**
     * 弹出，并删除头部元素
     */
    public static Integer poll() {
        while (queue.size()>1) {
            helper.offer(queue.poll());
        }
        Integer value = queue.poll();
        Queue<Integer> tmp = helper;
        helper = queue;
        queue = tmp;
        return value;
    }


    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            push(i);
        }

        while (!queue.isEmpty()) {
            System.out.print(poll() + " ");
        }

    }

}
